﻿using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System.Threading.Tasks;
using System.Windows;
using NLog;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
using System.Windows.Interop;

namespace Com.Lancetinc.Evs.Helpers
{
    public interface IMessageSupport
    {
        void Show(string msg, string caption = null);
        void ShowByMsgId(string msgId, string caption = null);

        void ShowWarning(string msg, string caption = null);
        void ShowWarningByMsgId(string msgId, string caption = null);

        void ShowError(string msg, string caption = null);
        void ShowErrorByMsgId(string msgId, string caption = null);

        Task<MessageBoxResult> ShowYesNoAsync(string msg, string caption = null);
        Task<MessageBoxResult> ShowYesNoByMsgIdAsync(string msgId, string caption = null);

        Task<MessageBoxResult> ShowOkCancelAsync(string msg, string caption = null);
        Task<MessageBoxResult> ShowOkCancelByMsgIdAsync(string msgId, string caption = null);


        Task<MessageBoxResult> ShowYesNoCancelAsync(string msg, string caption = null);
        Task<MessageBoxResult> ShowYesNoCancelByMsgIdAsync(string msgId, string caption = null);

    }

    public class DefaultMessageSupport : IMessageSupport
    {
        private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
        public void Show(string msg, string caption = null)
        {
            caption ??= Msgs.MsgBox_Prompt;
            Logger.Info($"I[{caption}]:{msg}");
            MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Information);
        }

        public void ShowByMsgId(string msgId, string caption = null)
        {
            Show(I18nHelper.Default.GetMessage(msgId), caption);
        }

        public void ShowWarning(string msg, string caption = null)
        {
            caption ??= Msgs.MsgBox_Warning;
            Logger.Info($"W[{caption}]:{msg}");
            MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        public void ShowWarningByMsgId(string msgId, string caption = null)
        {
            ShowWarning(I18nHelper.Default.GetMessage(msgId), caption);
        }

        public void ShowError(string msg, string caption = null)
        {
            caption ??= Msgs.MsgBox_Error;
            Logger.Info($"E[{caption}]:{msg}");
            MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Error);
        }

        public void ShowErrorByMsgId(string msgId, string caption = null)
        {
            ShowError(I18nHelper.Default.GetMessage(msgId), caption);
        }

        public Task<MessageBoxResult> ShowYesNoAsync(string msg, string caption = null)
        {
            caption ??= Msgs.MsgBox_Confirm;
            Logger.Info($"YN[{caption}]:{msg}");
            var result = MessageBox.Show(msg, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);
            return Task.FromResult(result);
        }

        public Task<MessageBoxResult> ShowYesNoByMsgIdAsync(string msgId, string caption = null)
        {
            return ShowYesNoAsync(I18nHelper.Default.GetMessage(msgId), caption);
        }

        public Task<MessageBoxResult> ShowOkCancelAsync(string msg, string caption = null)
        {
            caption ??= Msgs.MsgBox_Confirm;
            Logger.Info($"OC[{caption}]:{msg}");
            var result = MessageBox.Show(msg, caption, MessageBoxButton.OKCancel, MessageBoxImage.Question);
            return Task.FromResult(result);
        }

        public Task<MessageBoxResult> ShowOkCancelByMsgIdAsync(string msgId, string caption = null)
        {
            return ShowOkCancelAsync(I18nHelper.Default.GetMessage(msgId), caption);
        }

        /// <inheritdoc />
        public Task<MessageBoxResult> ShowYesNoCancelAsync(string msg, string caption = null)
        {
            caption ??= Msgs.MsgBox_Confirm;
            Logger.Info($"YNC[{caption}]:{msg}");
            var result = MessageBox.Show(msg, caption, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
            return Task.FromResult(result);
        }

        /// <inheritdoc />
        public Task<MessageBoxResult> ShowYesNoCancelByMsgIdAsync(string msgId, string caption = null)
        {
            return ShowYesNoCancelAsync(I18nHelper.Default.GetMessage(msgId), caption);
        }
    }

    public static class MessageHelper
    {
        private static IMessageSupport _proxy = new DefaultMessageSupport();

        public static void Init(IMessageSupport msgSupport)
        {
            _proxy = msgSupport;
        }

        public static void Show(string msg, string caption = null)
        {
            _proxy.Show(msg, caption);
        }

        public static void ShowByMsgId(string msgId, string caption = null)
        {
            _proxy.ShowByMsgId(msgId, caption);
        }

        public static void ShowWarning(string msg, string caption = null)
        {
            _proxy.ShowWarning(msg, caption);
        }

        public static void ShowWarningByMsgId(string msgId, string caption = null)
        {
            _proxy.ShowWarningByMsgId(msgId, caption);
        }

        public static void ShowError(string msg, string caption = null)
        {
            _proxy.ShowError(msg, caption);
        }

        public static void ShowErrorByMsgId(string msgId, string caption = null)
        {
            _proxy.ShowErrorByMsgId(msgId, caption);
        }

        public static Task<MessageBoxResult> ShowYesNoAsync(string msg, string caption = null)
        {
            return _proxy.ShowYesNoAsync(msg, caption);
        }

        public static Task<MessageBoxResult> ShowYesNoByMsgIdAsync(string msgId, string caption = null)
        {
            return _proxy.ShowYesNoByMsgIdAsync(msgId, caption);
        }

        public static Task<MessageBoxResult> ShowOkCancelAsync(string msg, string caption = null)
        {
            return _proxy.ShowOkCancelAsync(msg, caption);
        }

        public static Task<MessageBoxResult> ShowOkCancelByMsgIdAsync(string msgId, string caption = null)
        {
            return _proxy.ShowOkCancelByMsgIdAsync(msgId, caption);
        }

        public static Task<MessageBoxResult> ShowYesNoCancelAsync(string msg, string caption = null)
        {
            return _proxy.ShowYesNoCancelAsync(msg, caption);
        }

        public static Task<MessageBoxResult> ShowYesNoCancelByMsgIdAsync(string msgId, string caption = null)
        {
            return _proxy.ShowYesNoCancelByMsgIdAsync(msgId, caption);
        }
    }
}